﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using RelationTextBox;
using System.Reflection;
using System.Data.SqlClient;
using System.Threading;

namespace ERPForms
{
    public partial class ParteneriRezFrm : FrmRezumat
    {
        private Boolean ParteneriProprii = false;
        public long idPartenerSelectat = -1;
                
        private String _denumire = String.Empty;
        public String DenumirePartener
        {
            set
            {
                _denumire = value;
            }
        }

        /// <summary>
        /// Constructorul clasei
        /// </summary>
        /// <param name="mInfo">informatia despre modulul care apeleaza macheta</param>
        /// <param name="idSel">contine codul partenerului selectat daca este diferit de -1</param>
        public ParteneriRezFrm(ModuleClass mInfo,long idSel)
            : base(mInfo)
        {
            InitializeComponent();
            Init(mInfo, idSel);
        }

        private Boolean ParteneriFolositi = false;
        public ParteneriRezFrm(ModuleClass mInfo, long idSel,Boolean part_fol)
            : base(mInfo)
        {
            InitializeComponent();
            Init(mInfo, idSel);
            ParteneriFolositi = part_fol;
        }


        private void Init(ModuleClass mInfo, long idSel)
        {
            idPartenerSelectat = idSel;

            workConnString = mInfo.workConnectionString;
            eSRPDataSetParteneri.Parteneri.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            eSRPDataSetParteneri.Parteneri.ContColumn.DefaultValue = "N/A";
            eSRPDataSetParteneri.Parteneri.TipPartenerColumn.DefaultValue = "F";
            eSRPDataSetParteneri.Parteneri.NrMijlocTransportColumn.DefaultValue = String.Empty;
            eSRPDataSetParteneri.Parteneri.MijlocTransportColumn.DefaultValue = String.Empty;
            eSRPDataSetParteneri.Parteneri.NrAIColumn.DefaultValue = String.Empty;
            eSRPDataSetParteneri.Parteneri.SerieAIColumn.DefaultValue = string.Empty;
            eSRPDataSetParteneri.Parteneri.JudetRegComertColumn.DefaultValue = 0;
            eSRPDataSetParteneri.Parteneri.NrRegComertColumn.DefaultValue = 0;
            eSRPDataSetParteneri.Parteneri.AnRegComertColumn.DefaultValue = 0;
            ((RelationTextBoxCell)(furnizoriDGV.Columns["contColumn"].CellTemplate)).MyButtonClick += Cont_Click;

            furnizoriDGV.m_AutoCompletionColumns.Add(BancaColumn);
            furnizoriDGV.m_AutoCompletionColumns.Add(JudetColumn);
            furnizoriDGV.m_AutoCompletionColumns.Add(LocalitateColumn);

        }

        private void Cont_Click(object sender, EventArgs e)
        {
            FrmMainMdi frm = this.MdiParent as FrmMainMdi;
            if (frm == null)
                frm = StudioForm.staticMdiParent as FrmMainMdi;
            if (frm == null)
                return;
            Assembly asm = frm.GetModule("esrp");
            if (asm == null)
                return;
            String numeClasa = "ESRP.ConturiRezumatFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { this }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;

                newFrm.SelectionAddOn = true;
                if (newFrm.ShowDialog() == DialogResult.OK)
                {
                    furnizoriDGV.CurrentCell.Value =
                       newFrm.selectedRow.Cells["ContColumn"].Value;
                    if (furnizoriDGV.CurrentCell.IsInEditMode)
                        furnizoriDGV.EditingControl.Text = newFrm.selectedRow.Cells["ContColumn"].Value.ToString();        

                }
                newFrm.Close();
                newFrm.Dispose();

            }
        }

        private void FurnizoriFrm_Load(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = DeschideConexiune(workConnString);
                Object retObj = GetSetupValue("FaresSetup", "ParteneriProprii", ModuleInfo.CodUnitate, conn);
                if (retObj != null && retObj != DBNull.Value)
                    ParteneriProprii = Convert.ToBoolean(retObj);

                this.banciTableAdapter.Connection = conn;
                this.banciTableAdapter.Fill(this.eSRPDataSetParteneri.Banci);
                this.localitatiTableAdapter.Connection = conn;
                this.localitatiTableAdapter.Fill(this.eSRPDataSetParteneri.Localitati);
                this.judeteTableAdapter.Connection = conn;
                this.judeteTableAdapter.Fill(this.eSRPDataSetParteneri.Judete);
                this.parteneriTableAdapter.Connection = conn;
                if (!ParteneriFolositi)
                {
                    if (_denumire != String.Empty)
                    {
                        if (ParteneriProprii)
                            parteneriTableAdapter.FillByDenumireUnitate(this.eSRPDataSetParteneri.Parteneri, _denumire, ModuleInfo.CodUnitate);
                        else
                            this.parteneriTableAdapter.FillByDenumire(this.eSRPDataSetParteneri.Parteneri, _denumire);
                    }
                    else
                        if (!ParteneriProprii)
                            this.parteneriTableAdapter.Fill(this.eSRPDataSetParteneri.Parteneri);
                        else
                            this.parteneriTableAdapter.FillByUnitate(this.eSRPDataSetParteneri.Parteneri, ModuleInfo.CodUnitate);
                }
                else
                    this.parteneriTableAdapter.FillByParteneriFolositi(this.eSRPDataSetParteneri.Parteneri, ModuleInfo.CodUnitate, ParteneriFolositi);


                parteneriBindingSource.Sort = "Denumire";
                InchideConexiune(conn);

            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            this.listBndSourceMain.Add(parteneriBindingSource);
            this.listTableAdaptor.Add(parteneriTableAdapter.Adapter);

            
            HandleSalvare += Selectie;
            HandleTiparire += ParteneriRezFrm_HandleTiparire;
            AddButton("Tipareste", global::ERPStudio.Properties.Resources.tipareste, ParteneriRezFrm_HandleTiparire, ToolStripItemAlignment.Right, false);
            if (parteneriBindingSource.Count > 0)
            {
                if (idPartenerSelectat != -1)
                {
                    parteneriBindingSource.Position = parteneriBindingSource.Find("IDPartener", idPartenerSelectat);
                }
            }
        }

        void ParteneriRezFrm_HandleTiparire(object sender, EventArgs e)
        {
            PreviewRaportFrm frm = new PreviewRaportFrm("RaportFurnizori.rdlc", null, ModuleInfo);

            SqlConnection con = DeschideConexiune(workConnString);
            this.tiparesteParteneriTableAdapter.Connection = con;
            this.tiparesteParteneriTableAdapter.Fill(this.eSRPDataSetParteneri.TiparesteParteneri, ModuleInfo.CodUnitate,ParteneriFolositi);
            InchideConexiune(con);


            frm.AddDataSource("TiparesteParteneri", tiparesteParteneriBindingSource);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

       
    }
}
